package cn.edu.stu.service;

import cn.edu.stu.bean.AjaxResult;
import cn.edu.stu.bean.MicUser;
import cn.edu.stu.manager.AsyncManager;
import cn.edu.stu.manager.factory.AsyncFactory;
import cn.edu.stu.mapper.MicRecordMapper;
import cn.edu.stu.mapper.MicUserMapper;
import cn.edu.stu.tool.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Karl
 * @Date 2022/12/11 - 14:34
 */
@Service
public class UserService {

    @Autowired
    MicUserMapper userMapper;

    @Autowired
    ConfService confService;

    @Autowired
    MicRecordMapper recordMapper;

    public AjaxResult login(String userAct, String password){
        //生成令牌
        MicUser user = new MicUser();
        user.setUserAct(userAct);
        user.setUserPsw(password);
        MicUser micUser = userMapper.selectByUserNameAndUserPswEquals(user);
        if (micUser == null)
            return AjaxResult.error("账号不存在或密码错误！");
        micUser.setUserPsw(null);
        //登录成功，生成令牌
        Map<String,String > map = new HashMap<>();
        map.put("userid",micUser.getUserId().toString());
        map.put("username", micUser.getUserName());
        String token = JWTUtil.createToken(map,confService.getExpirationTime());
        AjaxResult success = AjaxResult.success("登录成功！");
        success.put("token",token);
        success.put("uid",micUser.getUserId());
        success.put("name",micUser.getUserName());
        return success;

    }


    public PageInfo<MicUser> getUserWithPage(int pageNum,int pageSize,int pageToShow){
        PageHelper.startPage(pageNum,pageSize);
        List<MicUser> users = userMapper.selectUserPage();
        return new PageInfo<>(users,pageToShow);
    }


    public AjaxResult register(MicUser user) {
        MicUser micUser = userMapper.selectByPrimaryKey(user.getUserId());
        try {
            if(micUser!=null) return AjaxResult.error("该uid已被绑定");
            if (userMapper.insert(user)==1) {
                return AjaxResult.success();
            }
        } catch (Exception e) {
            return AjaxResult.error("插入失败");
        }
        return AjaxResult.error("插入失败");
    }

    public void deleteByUid(long userId) {

        int i = userMapper.deleteByPrimaryKey(userId);
        if(i==1){
            List<Map<String, Object>> maps = recordMapper.selectByUid(userId);
            if(maps!=null&&maps.size()>0){
                int result = recordMapper.deleteByUserId(userId);
                if(result==maps.size()){
                    for (Map<String, Object> map : maps) {
                        AsyncManager.me().execute(AsyncFactory.deleteSongImg((String) map.get("imgPath")));
                        AsyncManager.me().execute(AsyncFactory.checkDeleteSongFile((Integer) map.get("songId")));
                    }
                }
            }

        }
    }
}
